﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;

namespace MHWPA
{
    public class AlbumDetailModel : ViewModelBase
    {
        private string _title;
        private string _summary;
        private string _image;

        public string Title
        {
            get
            {
                return _title;
            }
            set
            {
                _title = value;
                NotifyPropertyChanged("Title");
            }
        }
        public string Summary
        {
            get
            {
                return _summary;
            }
            set
            {
                _summary = value;
                NotifyPropertyChanged("Summary");
            }
        }

        public string Image
        {
            get
            {
                return _image;
            }
            set
            {
                _image = value;
                NotifyPropertyChanged("Image");
            }
        }
        public string Uri { get; set; }

        public ObservableCollection<SongItem> SongListItems { get; set; }

        private WebClient client;

        public void RefreshUri()
        {
            SongListItems.Clear();

            client = new WebClient();
            client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);

            client.DownloadStringAsync(new Uri(this.Uri));
        }

        void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            //if (e.Error == null)
            //try
            //{
                XElement xmlFeed = XElement.Parse(e.Result);
                
                this.Title = xmlFeed.Element("title").Value;
                this.Summary = xmlFeed.Element("summary").Value;

                foreach (XElement songs in xmlFeed.Descendants("songs"))
                {
                    foreach (XElement song in songs.Descendants("song"))
                    {
                        SongListItems.Add(
                            new SongItem()
                            {
                                Author = song.Element("author").Value,
                                Name = song.Element("name").Value,
                                Date = song.Element("date").Value,
                                Position = song.Element("position").Value,
                                Audio = song.Element("iphone_audio").Value,
                                Duration = song.Element("duration").Value
                            });
                    }
                }
            //}
            //catch { }
        }

        public class SongItem
        {
            public string Author { get; set; }
            public string Name { get; set; }
            public string Date { get; set; }
            public string Position { get; set; }
            public string Audio { get; set; }
            public string Duration { get; set; }
        }
    }
}
